<template>
	<el-form :model="formData" label-width="110px" label-position="left" ref="formRef" :rules="formRules" style="overflow: hidden !important;">
        <el-form-item label="上级类别：" prop="superiorId">
            <el-input v-model="parentName[0]" readonly @click.native="showLastType"></el-input>
            <el-input v-model="formData.superiorId" readonly style="display: none;"></el-input>
        </el-form-item>
        <el-form-item label="名　　称：" prop="name">
            <el-input v-model="formData.name" ref="ST1" @keyup.enter.native="enterKey($event, 'ST2')"></el-input>
        </el-form-item>
        <el-form-item label="助 记 码：" prop="mnemonicCode">
            <el-input v-model="formData.mnemonicCode" ref="ST2" @keyup.enter.native="enterKey($event, 'ST3')"></el-input>
        </el-form-item>
        <el-form-item label="备　　注：" prop="remakes">
            <el-input v-model="formData.remakes" ref="ST3" @keyup.enter.native="save('formRef')"></el-input>
        </el-form-item>
        <el-form-item label="连续增加：" style="margin-bottom: 10px !important;" v-if="!rowId">
            <el-checkbox v-model="continueAdd"></el-checkbox>
        </el-form-item>
        <el-form-item>
            <el-button size="small" type="primary" @click="save('formRef')">保存</el-button>
            <el-button size="small" type="danger" @click="close">关闭</el-button>
        </el-form-item>
    </el-form>
</template>
<script>
export default {
	data() {
		return {
			continueAdd: false,
			formData: {
				superiorId: ''
			},
			formRules: {
				name: [{required: true, message: '名称不能为空', triggle: 'blur'}]
			},
			parentName: this.$store.state.checkItemsStore.parentName,
			parentId: this.$store.state.checkItemsStore.parentId,           //上级部门id
		}
	},
	watch: {
        parentId(newVal) {
            this.formData['superiorId'] = newVal[0].toString();
        }
    },
    computed: {
    	rowId() {
        	return this.$store.state.checkItemsStore.rowId;
        }
    },
	methods: {
		getUpdateData() {			//获取当前修改数据
			this.$http.get('accountItem/selectInfo?id=' + this.rowId).then(res => {
				if(res.data.code == 20001) {
					if(res.data.data) {
						this.formData = res.data.data;
					}
				}
			});
		},
		showLastType() {			//打开上级类别窗口
			this.$util.openLayerIframe({title: '上级类别', zIndex: 13, width: '550px', height: '410px', url: this.$domain + '/#/basicData/checkItems/selectTypes'});
		},
		save(formName) {
			this.$refs[formName].validate((valid) => {
                if (valid) {
                	let data = {
                		name: this.formData['name'],
                		superiorId: this.formData['superiorId'],
                		mnemonicCode: this.formData['mnemonicCode'],
                		remakes: this.formData['remakes']
                	}
                	if(this.formData['id']) {
                		//修改
						data['id'] = this.formData['id'];
						data['state'] = this.formData['state'];
                        this.$http({
                            url: 'accountItem/update',
                            method: 'post',
                            params: data
                        }).then(res => {
                            if(res.data.code == 20001) {
                                this.$message({message: res.data.data || '修改成功', type: 'success', duration: 1500});
                                this.$store.commit('checkItemsStore/updateExpandNodeId', parseInt(data['superiorId']));
                                setTimeout(() => {
                                    this.$store.dispatch('checkItemsStore/getTreeData');        //刷新树形数据
                                    this.close();
                                }, 1000);
                            }
						});
                	} else {
						//新增
                		this.$http({
	                        url: 'accountItem/insert',
	                        method: 'post',
	                        params: this.formData
	                    }).then(res => {
	                    	if(res.data.code == 20001) {
	                    		this.$message({message: res.data.data || '新增成功', type: 'success', duration: 1500});
	                    		this.$store.commit('checkItemsStore/updateExpandNodeId', parseInt(data['superiorId']));
	                    		setTimeout(() => {
	                    			this.continueAdd ? this.$refs[formName].resetFields() : this.close();
	                    			this.$store.dispatch('checkItemsStore/getTreeData');        //刷新树形数据
	                    		}, 1000);
	                    	}
	                    });
                	}
                }
            });
		},
		close() {           //关闭新增弹窗
            let index = parent.layer.getFrameIndex(window.name);    //先得到当前iframe层的索引
            parent.layer.close(index);
        },
        enterKey(lastRef, nextRef) {		//回车键换行
            if(lastRef.target.value) {
                this.$refs[nextRef].focus();
            }
        }
	},
	created() {
		if(this.rowId) this.getUpdateData();
		let parentId = this.$store.state.checkItemsStore.parentId;
        this.formData['superiorId'] = parentId.toString();
	}
}
</script>